package webService.DAO;

import java.util.Date;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import webService.entities.Compte;
import webService.entities.Transaction;

//@Stateless
//@Local
public class CompteDAO {

	//@PersistenceContext(unitName = "JPABANKDB")
	private EntityManager em;
	
	public void create(Compte compte)
	{
		System.out.println("t null ?" + em);
		em.persist(compte);
		em.flush();
		
	}
	
	public Compte update(Compte compte)
	{
		return em.merge(compte);
		
	}
	
	public void delete(Compte compte)
	{
		Compte toRemoved = em.merge(compte);
		em.remove(toRemoved);
	}
	
	public Compte find(String nomDeCompte,
	String numero,
	String cryptogramme,
	Date datePerenption)
	{
		Compte result = null;
		String namedQuery = "SELECT distinct C from Compte C" +
				" where C.nomDeCompte = :nom and C.numero = :num and C.cryptogramme = :cryp and C.datePerenption = :date " ;
		try {
			Query query = em.createNamedQuery(namedQuery);

			query.setParameter("nom", nomDeCompte);
			query.setParameter("num", numero);
			query.setParameter("cryp", cryptogramme);
			query.setParameter("date", datePerenption);

			result = (Compte) query.getSingleResult();

		} catch (Exception e) {
			// todo
			e.printStackTrace();
		}

		return result;
	}

	public EntityManager getEm() {
		return em;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}
	
	
	
	
}
